package gui;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author Ramy Refky 
 * */
public class VerticalScrollBar extends JPanel implements MouseMotionListener {

	private static final long serialVersionUID = 1L;
	
	JLabel scrollButton;
	MainGui mg;
	WorkArea workArea;
	Actions actions;
	int scrollButtonHeight;
	double heightRatio;
	
	/**
	 * Constructor initializes Vertical Scroll bar.
	 * @param MainGui
	 */
	public VerticalScrollBar(MainGui mg){
		this.mg=mg;
		workArea=mg.getWorkArea();
		actions=mg.getActions();
		
		setBounds(906,50,10,500);
		this.setBackground(Color.white);
		setBorder(BorderFactory.createLineBorder(Color.gray,3));
		scrollButton = new JLabel();
		scrollButton.setOpaque(true);
		heightRatio= (workArea.getHeight()+0.0)/this.getHeight();
		
		scrollButtonHeight =(int) (493/heightRatio);
		scrollButton.setBounds(0,0,10,scrollButtonHeight);
		scrollButton.setVisible(true);
		scrollButton.setBackground(Color.gray.darker());
		scrollButton.setBorder(BorderFactory.createLineBorder(Color.gray,3));
		this.add(scrollButton);
		
		addMouseMotionListener(this);
		
		setVisible(true);
		setLayout(null);
		
	}
	/**
	 * Adjusts the width of the Scroll button according to the zoom factor.
	 * */
	public void refreshBar(){
		heightRatio= (workArea.getHeight()+0.0)/this.getHeight();
		scrollButtonHeight =(int) (493/heightRatio);
		scrollButton.setBounds(scrollButton.getX(),scrollButton.getY(),10,scrollButtonHeight);
		repaint();
	}
	@Override
	public void mouseDragged(MouseEvent arg0) {
		
		mg.getAboutPanel().setVisible(false);
		
		int yPosition= arg0.getY();
		int newPosition =(int) yPosition-scrollButtonHeight/2;
		int size= mg.getWorkArea().getWidth();
		
		if(newPosition<0){
			scrollButton.setBounds(0,0,10,scrollButtonHeight);
			workArea.setBounds(workArea.getX(),0,size,size);
		}
		else{
			if(newPosition>500-scrollButtonHeight){
				scrollButton.setBounds(0,500-scrollButtonHeight,10,scrollButtonHeight);
				workArea.setBounds(workArea.getX(),-size+500,size,size);
			}
			else{
				if(newPosition>=0 && newPosition<=500-scrollButtonHeight){
					scrollButton.setBounds(0,newPosition,10,scrollButtonHeight);
					workArea.setBounds(workArea.getX(),(int)(-newPosition*heightRatio),size,size);
				}
			}
		}	
	}
	@Override
	public void mouseMoved(MouseEvent arg0) {}
	
}
